package org.example.ui.log_registration;

import org.apache.commons.mail2.core.EmailException;
import org.example.Lock;
import org.example.business.LoginBusiness;
import org.example.business.UserBusiness;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MailBoxLoginPanel extends JPanel {
    private final JButton cancel = new JButton("取消");
    private final JButton login = new JButton("登录");
    private final JTextField account = new JTextField("邮箱",11);
    private final JTextField code = new JTextField(6);
    private final JLabel accountText = new JLabel("邮箱");
    private final JLabel codeText = new JLabel("验证码");
    private final JLabel passwordLogin = new JLabel("密码登录");
    private JLabel registration = new JLabel("注册");
    private final JLabel loginText = new JLabel("邮箱登录");
    private final JButton send = new JButton("发送");
    private final Font font = new Font("",Font.BOLD,15);
    private String strCode = "";
    private CardLayout cardLayout = null;
    private JPanel container = null;
    private ImageIcon background = null;
    public MailBoxLoginPanel(CardLayout cardLayout, JPanel container) {
        setLayout(null);
        setBounds(0,0,800,600);
        background = new ImageIcon("data/background/微信图片_20241127230536.jpg");
        this.cardLayout = cardLayout;
        this.container = container;
        setAll();
        setAllListener();
        addAll();
        setVisible(true);
    }
    private void setAll() {
        send.setFont(new Font("",10,15));
        send.setBounds(410,218,70,35);
        passwordLogin.setFont(font);
        registration = new JLabel("注册");
        registration.setFont(font);
        loginText.setFont(new Font("",10,30));
        loginText.setBounds(330,100,200,50);
        registration.setBounds(500,110,100,50);
        passwordLogin.setBounds(420,260,80,30);
        login.setFont(font);
        cancel.setFont(font);
        cancel.setBounds(260,320,80,40);
        account.setForeground(Color.LIGHT_GRAY);
        account.setFocusable(false);
        login.setBounds(450,320,80,40);
        account.setBounds(300,150,200,50);
        account.setFont(font);
        accountText.setFont(font);
        accountText.setBounds(250,150,50,50);
        codeText.setFont(font);
        codeText.setBounds(250,210,50,50);
        code.setBounds(300,210,100,50);
        code.setFont(font);
        code.setFocusable(false);
    }
    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(background.getImage(),0,0,800,600,null);
    }
    private void setAllListener() {
        code.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                code.setFocusable(true);
            }
        });
        account.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                account.setFocusable(true);
            }
        });
        account.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if(account.getText().equals("邮箱")) {
                    account.setText("");
                    account.setForeground(Color.BLACK);
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if(account.getText().equals("")) {
                    account.setText("邮箱");
                    account.setForeground(Color.LIGHT_GRAY);
                }
            }
        });
        send.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                try {
                    VerificationCode verificationCode = new VerificationCode();
                    verificationCode.sendCode(account.getText());
                    strCode = verificationCode.getCode();
                } catch (EmailException ex) {
                    JOptionPane.showMessageDialog(MailBoxLoginPanel.this, "邮箱错误");
                }
            }
        });
        registration.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                cardLayout.show(container,"0");
                account.setText("");
                code.setText("");
            }
        });
        passwordLogin.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                cardLayout.show(container,"1");
                account.setText("");
                code.setText("");
            }
        });
        code.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                code.setText("");
                code.setForeground(Color.BLACK);
            }

            @Override
            public void focusLost(FocusEvent e) {

            }
        });
        login.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (UserBusiness.mailBoxLogin(MailBoxLoginPanel.this,
                        account.getText(), code.getText(), strCode)) {
                        LoginBusiness.dialog.setVisible(false);
                        account.setText("");
                        account.setText("");
                }
            }
        });
        cancel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.exit(0);
            }
        });
    }
    private void addAll() {
        add(passwordLogin);
        add(account);
        add(codeText);
        add(accountText);
        add(registration);
        add(login);
        add(cancel);
        add(code);
        add(send);
        add(loginText);
    }
}
